﻿using CoffeeShop.BIZ;
using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;

namespace CoffeeShop.Controllers
{
    public class OfficeController : Controller
    {
        private OfficeBiz _officeBiz = new OfficeBiz();

        /// <summary>
        /// Page load all offices
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            List<Office> listAllOffices = new List<Office>();
            listAllOffices = _officeBiz.GetAllOffices();
            return View(listAllOffices);
        }

        /// <summary>
        /// Add new an office into Database
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Admin")]
        public string CreateOffice()
        {
            string officeName = Request.Params["txtOfficeName"].Trim();
            string officeAddress = Request.Params["txtOfficeAddress"].Trim();
            string officePhone = Request.Params["txtPhoneNumber"].Trim();
            if (_officeBiz.IsExistOffice(officeName))
            {
                return "ExistedOffice";
            }
            else
            {
                if (_officeBiz.AddOffice(officeName, officeAddress, officePhone))
                {
                    return "Successful";
                }
                else
                {
                    return "FailAddNewOffice";
                }
            }
        }

        /// <summary>
        /// Delete an office in Database
        /// </summary>
        /// <param name="officeId"></param>
        /// <returns></returns>
        public string DeleteOffice(string officeId)
        {
            var isDeleteSuccess = _officeBiz.DeleteOffice(Convert.ToInt32(officeId));
            if (isDeleteSuccess)
            {
                return "DeleteSuccessful";
            }
            else
            {
                return "FailDeleteOffice";
            }
        }

        /// <summary>
        /// Update an office
        /// </summary>
        public string UpdateOffice()
        {
            string officeId = Request.Params["txtUpdateOfficeId"].Trim();
            string officeName = Request.Params["txtUpdateOfficeName"].Trim();
            string officeAddress = Request.Params["txtUpdateOfficeAddress"].Trim();
            string officePhone = Request.Params["txtUpdatePhoneNumber"].Trim();

            if (_officeBiz.IsExistedWhenUpdate(Convert.ToInt32(officeId), officeName))
            {
                return "ExistedOfficeName";
            }
            else
            {
                if (_officeBiz.UpdateOffice(Convert.ToInt32(officeId), officeName, officeAddress, officePhone))
                {
                    return "Successful";
                }
                else
                {
                    return "FailUpdateOffice";
                }
            }
        }

        [HttpPost]
        [AllowAnonymous]
        public PartialViewResult LoadAllOffice()
        {
            List<Office> listAllOffices = new List<Office>();
            listAllOffices = _officeBiz.GetAllOffices();
            return PartialView("_OfficeTable", listAllOffices);
        }

        public JsonResult GetOfficeById(string officeId)
        {
            Office office = new Office();
            office = _officeBiz.GetOfficeById(Convert.ToInt32(officeId));
            return Json(new { ID = office.ID, Name = office.Name, Address = office.Address, PhoneNumber = office.PhoneNumber, IsDeleted = office.IsDeleted }
                , JsonRequestBehavior.AllowGet);
        }
    }
}
